package com.sjsoft.adapter;

import java.util.ArrayList;
import java.util.List;

import com.aphidmobile.utils.AphidLog;
import com.aphidmobile.utils.IO;
import com.sjsoft.app.video.R;
import com.sjsoft.application.chatVideoApp;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.novoda.imageloader.core.model.ImageTag;
import com.novoda.imageloader.core.model.ImageTagFactory;
import com.sjsoft.chat.video.ListVideo;
import com.sjsoft.constant.Constant;
import com.sjsoft.model.videoItem;
import com.sjsoft.video.LoadItemsVideo;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

public class VideoAdapter extends BaseAdapter implements Constant{

	private  ArrayList<videoItem> listPersonal;
	private LayoutInflater inflater;
	private Context _context;
	//public ImageLoader imageLoader;
	private DisplayImageOptions options;
	protected ImageLoader imageLoader = ImageLoader.getInstance();	
	private ImageTagFactory imageTagFactory;
	
	public void mergeData(VideoAdapter videoAdapter)
	{
		if(videoAdapter != null)
		{
			listPersonal.addAll(listPersonal.size(),videoAdapter.listPersonal);
			//notifyDataSetChanged();
		}
	}
	
	public void addItems(ArrayList<videoItem> items)
	{
		listPersonal.addAll(listPersonal.size(),items);
	}
	
	public VideoAdapter(Context context) {
		inflater = LayoutInflater.from(context);
		imageTagFactory = ImageTagFactory.getInstance(context, R.drawable.loading);
		_context = context;
		listPersonal = new ArrayList<videoItem>();
		//imageLoader=new ImageLoader(context);
		options = new DisplayImageOptions.Builder()		
		.imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
	}
	
	@Override
	public int getCount() {
		int nSize = listPersonal.size();
		if(nSize % 2 == 1)
			nSize++;		
		return nSize/2;
	}


	@Override
	public long getItemId(int position) {		
		return position;
		
	}
	
	
	@Override
	public Object getItem(int position) {
		return position;
	}
	
	public int getRealItem()
	{
		return listPersonal.size();
	}
	
	static class videoViewHolder {
        protected ImageView image1;	       
        protected TextView  name1;
        protected TextView  address1;
        protected TextView  age1;
        protected TextView  height1;
        protected TextView  weight1;
        protected TextView  desc1;
        protected TextView  price1;
        protected ProgressBar spinner1;
        
        
        protected ImageView image2;	       
        protected TextView  name2;
        protected TextView  address2;
        protected TextView  age2;
        protected TextView  height2;
        protected TextView  weight2;
        protected TextView  desc2;
        protected TextView  price2;
        protected ProgressBar spinner2;
        
    }
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View layout = convertView;
		videoViewHolder viewHolder = null;
		if (convertView == null)
		{
			layout = inflater.inflate(R.layout.personal_layout, null);
			viewHolder = new videoViewHolder();
			
			//binding viewholder with layout control		
			viewHolder.image1 = (ImageView) layout.findViewById(R.id.img_personal1);
			viewHolder.name1 = (TextView) layout.findViewById(R.id.name1);
			viewHolder.address1 = (TextView)layout.findViewById(R.id.txt_address1);			
			viewHolder.age1 = (TextView) layout.findViewById(R.id.txt_age1);
			viewHolder.height1 = (TextView) layout.findViewById(R.id.txt_height1);
			viewHolder.weight1 = (TextView) layout.findViewById(R.id.txt_weight1);
			viewHolder.desc1 = (TextView) layout.findViewById(R.id.txt_desc1);
			viewHolder.price1 = (TextView) layout.findViewById(R.id.txt_price1);
			viewHolder.spinner1 = (ProgressBar) layout.findViewById(R.id.spinner1);
			
			viewHolder.image2 = (ImageView) layout.findViewById(R.id.img_personal2);
			viewHolder.name2 = (TextView) layout.findViewById(R.id.name2);
			viewHolder.address2 = (TextView)layout.findViewById(R.id.txt_address2);			
			viewHolder.age2 = (TextView) layout.findViewById(R.id.txt_age2);
			viewHolder.height2 = (TextView) layout.findViewById(R.id.txt_height2);
			viewHolder.weight2 = (TextView) layout.findViewById(R.id.txt_weight2);
			viewHolder.desc2 = (TextView) layout.findViewById(R.id.txt_desc2);
			viewHolder.price2 = (TextView) layout.findViewById(R.id.txt_price2);
			viewHolder.spinner2 = (ProgressBar) layout.findViewById(R.id.spinner2);
			
			layout.setTag(viewHolder);           
			
		}
		else
		{
			viewHolder = (videoViewHolder) layout.getTag();
		}
		
		//set data
		if(viewHolder != null)
		{
			final videoItem video1 = listPersonal.get(position*2);
			if(video1 != null)
			{

				final ProgressBar spinner = viewHolder.spinner1;
				final ImageView image1 = viewHolder.image1;
				
				ImageTag tag = imageTagFactory
						.build(DOWNLOAD_SERVER+ video1.getIdIcon());
				viewHolder.image1.setTag(tag);
				viewHolder.image1.setTag(R.id.tag_async_image_view_position, position);
				ListVideo.getImageManager().getLoader().load(viewHolder.image1);
								
				viewHolder.name1.setText(AphidLog.format("%s", video1.getName()));
				
				viewHolder.address1.setText(getFormatText("Địa chỉ: ",video1.getAddress()));			
				viewHolder.age1.setText(getFormatText("Tuổi: ", video1.getAge()));
				viewHolder.height1.setText(getFormatText("Chiều cao: ", video1.getHeight()));
				viewHolder.weight1.setText(getFormatText("Cân nặng: ",  video1.getWeight()));
				viewHolder.desc1.setText(getFormatText("Thông tin: ", video1.getDesc()));
				int nAmout = video1.getAmount();
				if(nAmout == 0)
					viewHolder.price1.setText(getFormatText("US :", "Free"));
				else
					viewHolder.price1.setText(getFormatText("US :", ""+nAmout));
				
				LinearLayout layoutPersonal1 = (LinearLayout)layout.findViewById(R.id.personal1);			
				layoutPersonal1.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub				
						((ListVideo)_context).startChating(video1);
					}
				});			
				
			}
			
			if(listPersonal.size() > position*2+1)
			{
				final videoItem video2 = listPersonal.get(position*2+1);
				
				if(video2 != null)
				{
					final ProgressBar spinner = viewHolder.spinner2;
					final ImageView image2 = viewHolder.image2;
					ImageTag tag = imageTagFactory
							.build(DOWNLOAD_SERVER+ video2.getIdIcon());
					viewHolder.image2.setTag(tag);
					viewHolder.image2.setTag(R.id.tag_async_image_view_position, position);
					ListVideo.getImageManager().getLoader().load(viewHolder.image2);
					
					viewHolder.name2.setText(AphidLog.format("%s", video2.getName()));
					
					viewHolder.address2.setText(getFormatText("Địa chỉ: ",video2.getAddress()));			
					viewHolder.age2.setText(getFormatText("Tuổi: ", video2.getAge()));
					viewHolder.height2.setText(getFormatText("Chiều cao: ", video2.getHeight()));
					viewHolder.weight2.setText(getFormatText("Cân nặng: ",  video2.getWeight()));
					viewHolder.desc2.setText(getFormatText("Thông tin: ", video2.getDesc()));
					int nAmout2 = video2.getAmount();
					if(nAmout2 == 0)
						viewHolder.price2.setText(getFormatText("US :", "Free"));
					else
						viewHolder.price2.setText(getFormatText("US :", ""+nAmout2));
					LinearLayout layoutPersonal1 = (LinearLayout)layout.findViewById(R.id.personal2);
					
					layoutPersonal1.setOnClickListener(new View.OnClickListener() {
					
						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub				
							((ListVideo)_context).startChating(video2);
						}
					});
				
				}
			}
		}
		
		return layout;
	}
		
	private Spanned getFormatText(String label , String sContent)
	{
		return Html.fromHtml("<font color=#000000>"+label + "</font>" +"<font color=#FF0AFF>" + sContent + "</font>");
	}
}

